package com.topisv.tms.service.ttkdex;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.topisv.tms.entity.EdiOrder;
import com.topisv.tms.entity.ExpressConfigure;
import com.topisv.tms.entity.ediOrder.EdiOrderVO;
import com.topisv.tms.repository.ExpressConfigureDao;
import com.topisv.tms.service.base.BaseService;
import com.topisv.tms.utils.HttpClientUtil;
import com.topisv.tms.utils.ttkd.DayMailConstants;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 *TTKDEX的业务逻辑类
 *  Date          Author       Version       Description
 * ----------------------------------------------------------
 *  2016-1-15      YANLEI            1.0           Create
 */
@Component
public class TTKDEXService extends BaseService {
	@Autowired
	private ExpressConfigureDao expressConfigureDao;

	/**
	 * 校验是否可达
	 * @param order
	 * @return boolean
	 */
	public boolean checkArrive(EdiOrder order) {
		boolean rs = false;
		try {
			String charset = "utf-8";
			
			ExpressConfigure expressConfigure = null;
			expressConfigure = expressConfigureDao.findByWarehouseAndCompany(order.getWarehouseCode(), "TTKDEX");
			String url = expressConfigure.getUrl();
			String site = expressConfigure.getCompanyName();
			String cus = expressConfigure.getWarehouseName();
			String password = expressConfigure.getPassword();
			
			/*String url = "http://api.ttk.cn/serverProxy/ProxyActionEx.php";
			String site = "总部";
			String cus = "测试客户";
			String password = "1234";*/
			
			JSONObject json = new JSONObject();
			json.put("site", site);
			json.put("cus", cus);
			json.put("password", password);
			
			HashMap<String, String> paramMap2 = new HashMap<String, String>();
			paramMap2.put("key", order.getOrdeno());
			paramMap2.put("prov", order.getProvna());
			paramMap2.put("city", order.getCityna());
			paramMap2.put("area", order.getDistna());
			paramMap2.put("addr", order.getAddres().replace(order.getProvna()+order.getCityna()+order.getDistna(), ""));
			List list = new ArrayList();
			list.add(paramMap2);
			json.put("data", list);
			HashMap<String, String> paramMap = new HashMap<String, String>();
			paramMap.put(DayMailConstants.PARAM_ONE,DayMailConstants.PARAM_ONE_VALUE);
			paramMap.put(DayMailConstants.PARAM_TWO,DayMailConstants.PARAM_CHECK);
			paramMap.put(DayMailConstants.PARAM_THREE,json.toString());
			String data = HttpClientUtil.doPost(url, charset, paramMap);
			log.info("天天区域接口返回:{}", data);
			JSONArray jsonArray = JSONArray.fromObject(data);
			JSONObject obj = (JSONObject)jsonArray.get(0);
			return "0".equals(obj.getString("result"));
		} catch (Exception e) {
			log.error("some error.", e);
		}
		return rs;
	}
	
	/**
	 * 校验是否可达
	 * @author huwenjun
	 * @param order
	 * @return boolean
	 */
	public Map<String, Integer> checkArrive(EdiOrderVO order) {
	    Map<String, Integer> result = new HashMap<String, Integer>();
	    try {
			String charset = "utf-8";
			
			ExpressConfigure expressConfigure = null;
			expressConfigure = expressConfigureDao.findByWarehouseAndCompany(order.getWarehouse_code(), "TTKDEX");
			String url = expressConfigure.getUrl();
			String site = expressConfigure.getCompanyName();
			String cus = expressConfigure.getWarehouseName();
			String password = expressConfigure.getPassword();
			
			JSONObject json = new JSONObject();
			json.put("site", site);
			json.put("cus", cus);
			json.put("password", password);
			
			HashMap<String, String> paramMap2 = new HashMap<String, String>();
			paramMap2.put("key", order.getOrdeno());
			paramMap2.put("prov", order.getProvna());
			paramMap2.put("city", order.getCityna());
			paramMap2.put("area", order.getDistna());
			paramMap2.put("addr", order.getAddres().replace(order.getProvna()+order.getCityna()+order.getDistna(), ""));
			List list = new ArrayList();
			list.add(paramMap2);
			json.put("data", list);
			HashMap<String, String> paramMap = new HashMap<String, String>();
			paramMap.put(DayMailConstants.PARAM_ONE,DayMailConstants.PARAM_ONE_VALUE);
			paramMap.put(DayMailConstants.PARAM_TWO,DayMailConstants.PARAM_CHECK);
			paramMap.put(DayMailConstants.PARAM_THREE,json.toString());
			String data = HttpClientUtil.doPost(url, charset, paramMap);
			log.info("天天区域接口返回:{}", data);
			JSONArray jsonArray = JSONArray.fromObject(data);
			JSONObject obj = (JSONObject)jsonArray.get(0);
			if ("0".equals(obj.getString("result"))) {
                result.put("arrive", 1);
            }else{
                result.put("arrive", 2);
            }
			return result;
		} catch (Exception e) {
			log.error("调用天天接口判断超区错误：天天系统崩溃，返回报文非法。", e);
			return null;
		}
	}
	
	public static void main(String args[])
	{
		EdiOrder o = new EdiOrder();
		o.setOrdeno("KEY0001");
		o.setProvna("浙江省");
		o.setCityna("杭州市");
		o.setDistna("其他区");
		o.setAddres("海威大楼");
		//System.out.println(checkArrive(o));
	}

}
